﻿import QtQuick 2.9
import QtQuick.Controls 2.2


ShaderEffect {
    id: r
    readonly property int directHorizon: 0
    readonly property int directVertical: 1
    readonly property int directHorizonReverse: 2
    readonly property int directVerticalReverse: 3

    property int dir: directVertical//directHorizon

    property int count: 4
    property int duration: 5000
    property ShaderEffectSource effectSource: ShaderEffectSource {
        hideSource: true
    }
    property int percent
    readonly property int perLen: 100
    NumberAnimation {
        id: animation
        target: r
        property: "percent"
        from: 0
        to: 100
        alwaysRunToEnd: true
        loops: 1
        duration: r.duration
    }

    //fragmentShader: CusEffectCommon.fragmentShaderCommon + "
    fragmentShader: "
    #version 100
varying lowp vec2 qt_TexCoord0;
uniform lowp float qt_Opacity;
uniform sampler2D effectSource;
uniform int percent;
uniform int perLen;
uniform int count;
uniform int dir;

lowp float  mymod(lowp float  x, lowp float  y)
{
    return x - y * floor(x / y);
}

lowp float mystep(lowp float edge, lowp float x) {
    return x < edge ? 0.0 : 1.0;
}

void main()
{
    int percents[4];
    highp float coords[4];
    highp float alphas[4];

    percents[0] = percent;
    percents[1] = percent;
    percents[2] = perLen - percent;
    percents[3] = perLen - percent;

    coords[0] = qt_TexCoord0.x;
    coords[1] = qt_TexCoord0.y;
    coords[2] = qt_TexCoord0.x;
    coords[3] = qt_TexCoord0.y;

    lowp vec4 color = texture2D(effectSource, qt_TexCoord0);
    int len = int(coords[dir] * float(perLen) * float(count));

    lowp float alpha =  mystep(float(percents[dir]), mymod(float(len), float(perLen)));

    alphas[0] = 1.0 - alpha;
    alphas[1] = 1.0 - alpha;
    alphas[2] = alpha;
    alphas[3] = alpha;

    alpha = qt_Opacity * alphas[dir];
    gl_FragColor = vec4(color.rgb * alpha, alpha);
}
    "

    function restart() {
        animation.restart()
    }

}
